package com.itboye.lottery.activity;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.handmark.pulltorefresh.library.ObservableScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.follow.HotPeopleInfoActivity;
import com.itboye.lottery.adapter.MyAttentionAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.bean.OutWrapBean;
import com.itboye.lottery.bean.UserDataBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class MyAttentionActivity extends BaseActivity implements ResponseCallback {

    RecyclerView rl_my_attention;
    UserPresenter userPresenter = new UserPresenter(this);
    int page = 1, pageSize = 10, follow = 0;//0:关注  1：粉丝
    private ArrayList<UserDataBean> userDataBeanArrayList = new ArrayList<>();
    private MyAttentionAdapter adapter;
    private int position = 0;
    PullToRefreshScrollView pull_to_refresh_scrollView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_attention;
    }

    @Override
    protected void initData() {
        follow = getIntent().getIntExtra("follow", 0);
        initPullToRefresh();
        rl_my_attention.setLayoutManager(new LinearLayoutManager(this));
        rl_my_attention.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new MyAttentionAdapter(this, userDataBeanArrayList);
        adapter.setFollow(follow);
        adapter.setOnItemClickListener((view, position) -> {
            startActivity(new Intent(MyAttentionActivity.this, HotPeopleInfoActivity.class).putExtra("userId", userDataBeanArrayList.get(position).getId() + ""));
        });
        rl_my_attention.setAdapter(adapter);
    }

    private void initPullToRefresh() {
        pull_to_refresh_scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        pull_to_refresh_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ObservableScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page = 1;
                getMyAttentionList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page++;
                getMyAttentionList();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMyAttentionList();
    }

    private void getMyAttentionList() {
        userPresenter.getMyAttentionList(page, pageSize, follow);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_item_attention_click:
                position = (int) view.getTag();
                userPresenter.follow(userDataBeanArrayList.get(position).getId(), 2);
                break;
        }
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        if (requestSuccessType == userPresenter.getMyAttentionList_SUCCESS) {
            if (page == 1) {
                userDataBeanArrayList.clear();
            }
            if (((OutWrapBean) o).getList() != null) {
                userDataBeanArrayList.addAll(((OutWrapBean) o).getList());
                adapter.notifyDataSetChanged();
            }
        } else if (requestSuccessType == userPresenter.follow_SUCCESS) {
            ByAlert.alert("取消关注成功");
            userDataBeanArrayList.remove(position);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        ByAlert.alert(msg);
    }
}
